/**
 * Description:一些注释<br/>
 * CreateTime:2019-03-26<br/>
 * Creater: yangpengfei360@126.com <br/>
 * @author yangpengfei
 */
package cn.cnsy123.bookwebsiteserver.service.user.impl;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.cnsy123.bookwebsiteserver.entity.SysLoginHistoryInfo;
import cn.cnsy123.bookwebsiteserver.entity.SysUser;
import cn.cnsy123.bookwebsiteserver.param.UserLoginParam;
import cn.cnsy123.bookwebsiteserver.repository.LoginDao;
import cn.cnsy123.bookwebsiteserver.repository.SysLoginHistoryInfoDao;
import cn.cnsy123.bookwebsiteserver.service.user.LoginService;
import cn.cnsy123.bookwebsiteserver.util.JsonUtil;
import cn.cnsy123.bookwebsiteserver.util.MD5PassWordEncoder;

/**
 * Description:一些注释<br/>
 * CreateTime:2019-03-26<br/>
 * Creater: yangpengfei360@126.com <br/>
 * @author yangpengfei
 */
@Service
public class LoginServiceImpl implements LoginService{
    
    private static final Logger logger = LoggerFactory.getLogger(LoginServiceImpl.class);
    
    @Autowired
    private LoginDao loginDao;
    @Autowired
    private SysLoginHistoryInfoDao sysLoginHistoryInfoDao;
    
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Map<String, String> doLogin(UserLoginParam userLoginParam,String requestIP,String nowVerifyCode) {
        Map<String, String> result = new HashMap<String, String>();
        String errorMessage = "登录成功";
        String view = "login";
        try {
            
            if(userLoginParam == null) {
                errorMessage = "用户名或密码为空";
            }else {
                if(StringUtils.isNotBlank(userLoginParam.getUserName()) 
                        && StringUtils.isNotBlank(userLoginParam.getPassWord())
                        && StringUtils.isNotBlank(userLoginParam.getVertifyCode())) {
                    if(StringUtils.equalsIgnoreCase(userLoginParam.getVertifyCode(), nowVerifyCode)) {
                        SysUser sysUser = loginDao.findByUserName(userLoginParam.getUserName());
                        if(sysUser != null) {
                            if(MD5PassWordEncoder.md5Hex(userLoginParam.getPassWord()).equals(sysUser.getPassWord())) {
                                result.put("cacheInfo", JsonUtil.obj2jsonStr(sysUser));
                                view = "index";
                            }else {
                                errorMessage = "登录密码错误";
                            }
                        }else {
                            errorMessage = "用户名不存在";
                        }
                    }else {
                        errorMessage = "验证码输入错误";
                    }
                    
                }else {
                    if(StringUtils.isBlank(userLoginParam.getUserName())) {
                        errorMessage = "用户名为空";
                    }else if(StringUtils.isBlank(userLoginParam.getPassWord())) {
                        errorMessage = "密码为空";
                    }else if(StringUtils.isBlank(userLoginParam.getVertifyCode())) {
                        errorMessage = "验证码为空";
                    }
                }
            }
            
        } catch (Exception e) {
            errorMessage = e.getMessage();
        }finally {
            //记录登录日志
            SysLoginHistoryInfo sysLoginHistoryInfo = new SysLoginHistoryInfo();
            sysLoginHistoryInfo.setOptMessage(errorMessage);
            sysLoginHistoryInfo.setOptUser(userLoginParam.getUserName());
            sysLoginHistoryInfo.setRecordTime(Calendar.getInstance().getTime());
            sysLoginHistoryInfo.setOptLastIP(requestIP);
            logger.info("用户:[{}],登录操作结果:[{}]",userLoginParam.getUserName(),sysLoginHistoryInfo);
            sysLoginHistoryInfoDao.save(sysLoginHistoryInfo);
        }
        result.put("view", view);
        result.put("errorMessage", errorMessage);
        return result;
    }

    

}
